/*
 * This is Melet, a salary slip designer. Feed it with data regarding to salary payment, prepare it for print through the graphical presentation, and you have it done.
 * Copyright (C) 2014, AFDiaX
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * This program is based on UMLet, a Free UML Tool for Fast UML Diagrams.
 * To contact the authors of UMLet visit https://code.google.com/p/umlet/people/list
 * To contact the authors of Melet visit https://gna.org/users/gabocze
 */
package com.baselet.diagram;

import java.awt.AlphaComposite;
import java.awt.Composite;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.font.FontRenderContext;

import javax.swing.JComponent;

import com.baselet.control.basics.geom.DimensionDouble;
import com.baselet.control.basics.geom.Rectangle;

public class DiagramNotification extends JComponent {

	private static final long serialVersionUID = 1L;
	private final String message;
	private final Rectangle drawPanelSize;

	private static final Font notificationFont = new Font(Font.SANS_SERIF, Font.PLAIN, 10);
	private static final FontRenderContext frc = new FontRenderContext(null, true, true);

	public DiagramNotification(Rectangle drawPanelSize, String message) {
		this.message = message;
		this.drawPanelSize = drawPanelSize;
		this.setSize(100, 20);
		adaptDimensions();
	}

	@Override
	public final void paint(Graphics g) {

		super.paint(g);
		Graphics2D g2 = (Graphics2D) g;
		Composite old = g2.getComposite(); // Store non-transparent composite

		g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f)); // 40% transparency
		g2.drawRect(0, 0, getWidth() - 1, getHeight() - 1);

		Font drawFont = g2.getFont();
		g2.setFont(notificationFont);
		adaptDimensions();

		int textX = 5;
		int textY = getHeight() / 2 + 3;
		g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.7f)); // 70% transparency
		g2.drawString(message, textX, textY);

		g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.05f)); // 5% transparency
		g2.setColor(java.awt.Color.blue);
		g2.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
		g2.setComposite(old);
		g2.setFont(drawFont);
	}

	private void adaptDimensions() {
		DimensionDouble textSize = FontHandler.getTextSizeStatic(new FormattedFont(message, notificationFont.getSize(), notificationFont, frc));
		int x = (int) (drawPanelSize.getX2() - textSize.getWidth() - 20);
		int y = drawPanelSize.getY() + 10;
		this.setLocation(x, y);
		this.setSize((int) textSize.getWidth() + 10, (int) textSize.getHeight() + 10);
	}
}
